
/*
 * Carrot2 project.
 *
 * Copyright (C) 2002-2011, Dawid Weiss, Stanisław Osiński.
 * All rights reserved.
 *
 * Refer to the full license file "carrot2.LICENSE"
 * in the root folder of the repository checkout or at:
 * http://www.carrot2.org/carrot2.LICENSE
 */

package org.carrot2.examples.clustering;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.carrot2.clustering.lingo.LingoClusteringAlgorithm;
import org.carrot2.core.Cluster;
import org.carrot2.core.Controller;
import org.carrot2.core.ControllerFactory;
import org.carrot2.core.Document;
import org.carrot2.core.IDocumentSource;
import org.carrot2.core.ProcessingResult;
import org.carrot2.examples.ConsoleFormatter;
import org.xmlParser.typeOf;


/**
 * This example shows how to cluster a set of documents available as an {@link ArrayList}.
 * This setting is particularly useful for quick experiments with custom data for which
 * there is no corresponding {@link IDocumentSource} implementation. For production use,
 * it's better to implement a {@link IDocumentSource} for the custom document source, so
 * that e.g., the {@link Controller} can cache its results, if needed.
 * 
 * @see ClusteringDataFromDocumentSources
 * @see UsingCachingController
 */
public class clusterDocumentsLingo
{
	
	public ConsoleFormatter abc;
    //public static void main(String args[]) throws Exception
	public StringBuffer clusterDocuments(String Q, int L, String on, String source, boolean postProcessingCluster, boolean postProcessingCapCount, boolean intent, int intentLimit,float postClusterCoverage,boolean reMerge,boolean galleries, boolean postProcess,boolean logLookup, boolean time,long month,long date,long hours,boolean debug) throws Exception 
    {        
    	
    	boolean queryType;		//to check if the specified query lies in the whitelist/stop list of some specfic queries
    	StringBuffer ret=new StringBuffer();
            	
    
    	
    	if(time){
    		//time range query
    		
    		ret=timerange( Q,  L,  on,  source,  postProcessingCluster,  postProcessingCapCount,  intent , intentLimit, postClusterCoverage,reMerge, galleries,postProcess,logLookup,month,date, hours);
    		
    	}
    	else{
    		
    	
        
            typeOf qType =new typeOf();
            String qintent=qType.predictIntent(Q);
            
            if(qintent!=null){
            	//the query has some type...
                
                
                whiteList wh = new whiteList();
                
                HashMap<String, Boolean> list = wh.readWhiteList("stopEntities");
                
                if(list.containsKey(qintent)){
                	System.out.println("Query in the specified list");
                	
                	ret=specificQuery( Q,  L,  on,  source, postProcessingCluster, postProcessingCapCount,  intent,  intentLimit, postClusterCoverage, reMerge, galleries);
                }
                else{
                	System.out.println("Normal Query Execution");
                	
                	ret=normalQuery( Q,  L,  on,  source, postProcessingCluster, postProcessingCapCount,  intent,  intentLimit, postClusterCoverage, reMerge, galleries,postProcess,logLookup,debug);
                }
                
               }
                else{
                	// not a normal keyword query make a blank guess run normal execution
                	
                	System.out.println("keyord not normal:: no such specified type");
                	
                	ret=normalQuery( Q,  L,  on,  source, postProcessingCluster, postProcessingCapCount,  intent,  intentLimit, postClusterCoverage, reMerge, galleries,postProcess,logLookup,debug);
                	
                	System.out.println(ret.length());
                	
                	if (ret.length()==0)
                		ret.append("Sorry no such keyword");
                	
                	}
            
    	}    
            return ret;
            
    }
	
	
	
	public StringBuffer timerange(String Q, int L, String on, String source, boolean postProcessingCluster, boolean postProcessingCapCount, boolean intent, int intentLimit,float postClusterCoverage,boolean reMerge,boolean galleries, boolean postProcess,boolean logLookup,long month,long day,long hours)throws Exception{
		StringBuffer ret=new StringBuffer();
		
	    URLConnectionReader obj = new URLConnectionReader(Q,L);
	    
	    
        
        ArrayList<ArrayList<String>> data1=obj.xmlToDocArrayTime(month,day,hours);
        
        
        final ArrayList<Document> documents = new ArrayList<Document>();
        
        for (ArrayList<String> row : data1)
        {
            //documents.add(new Document(row[1], row[2], row[0]));
        	documents.add(new Document(row.get(2),row.get(1),row.get(0)));            
        }

        /* A controller to manage the processing pipeline. */
        
        
        final Controller controller = ControllerFactory.createSimple();
        
        final ProcessingResult byTopicClusters = controller.process(documents, Q,LingoClusteringAlgorithm.class);
        
        final List<Cluster> clustersByTopic = byTopicClusters.getClusters();
        
        
        
        ret.append("<html><head><script type='text/javascript' src='/lib/jquery-1.7.2.min.js'></script><script type='text/javascript' src='/lib/jquery.mousewheel-3.0.6.pack.js'></script><script type='text/javascript' src='/source/jquery.fancybox.js?v=2.0.6'></script><link rel='stylesheet' type='text/css' href='/source/jquery.fancybox.css?v=2.0.6' media='screen' />	<link rel='stylesheet' type='text/css' href='/source/helpers/jquery.fancybox-buttons.css?v=1.0.2' />	<script type='text/javascript' src='/source/helpers/jquery.fancybox-buttons.js?v=1.0.2'></script>	<link rel='stylesheet' type='text/css' href='/source/helpers/jquery.fancybox-thumbs.css?v=1.0.2' />	<script type='text/javascript' src='/source/helpers/jquery.fancybox-thumbs.js?v=1.0.2'></script><script type='text/javascript' src='/source/helpers/jquery.fancybox-media.js?v=1.0.0'></script><script>$(document).ready(function() {$('.fancybox').fancybox({	openEffect	: 'none',	closeEffect	: 'none',nextEffect	: 'none',prevEffect	: 'none'});});</script></head><body>");
        
        ret.append(obj.url);
        
        ret.append("<script>function display(e){var obj=document.getElementById(e);if(obj.style.display=='block'){obj.style.display='none';}else{obj.style.display='block';}}</script>");
        
        abc=new ConsoleFormatter(Q,obj.thumbToUrl);
        
        if(postClusterCoverage>0.0f)abc.setPostClusterCoverage(postClusterCoverage);
        
        abc.setPostProcessingCluster(postProcessingCluster);
        
        abc.setLabelStiching(postProcess);
        
        abc.setLogLookup(logLookup);
        
        abc.setPostProcessingCapCount(postProcessingCapCount);
        
        abc.setReMerge(reMerge);
        
        //setting the galleries flag
        abc.setDispGalleries(galleries);
        
        ret.append(abc.displayClusters(clustersByTopic));
        
        ret.append("</body></html>");
        
        return (ret);
        
        
    
	}
		
	public StringBuffer normalQuery(String Q, int L, String on, String source, boolean postProcessingCluster, boolean postProcessingCapCount, boolean intent, int intentLimit,float postClusterCoverage,boolean reMerge,boolean galleries, boolean postProcess,boolean logLookup,boolean debug)throws Exception
		{
			
		
		    String query=Q;
            int limit=L;
            ArrayList<String> titles = new ArrayList<String>();
        
			StringBuffer ret=new StringBuffer();
			
            URLConnectionReader obj = new URLConnectionReader(query,limit,intent);
            
            ArrayList<ArrayList<String>> data1=obj.xmlToDocArray(on, source, intentLimit);
            
            
            
            
            final ArrayList<Document> documents = new ArrayList<Document>();
            
            
            for (ArrayList<String> row : data1)
            {
                //documents.add(new Document(row[1], row[2], row[0]));
            	documents.add(new Document(row.get(2),row.get(1),row.get(0)));
            	
            }
            
            for (ArrayList<String> row : data1)
            {
                //documents.add(new Document(row[1], row[2], row[0]));
            	
            	titles.add(row.get(2));
            	
            	
            }
            
            
            coOccurence gram = new coOccurence();
            ArrayList<coOccurence.entity> grams = gram.coOccuringWordsArrayList(titles);
            
            
            
            

            /* A controller to manage the processing pipeline. */
            
            
            final Controller controller = ControllerFactory.createSimple();
            
            final ProcessingResult byTopicClusters = controller.process(documents, Q,LingoClusteringAlgorithm.class);
            
            final List<Cluster> clustersByTopic = byTopicClusters.getClusters();
            
            
            
            ret.append("<html><head><script type='text/javascript' src='/lib/jquery-1.7.2.min.js'></script><script type='text/javascript' src='/lib/jquery.mousewheel-3.0.6.pack.js'></script><script type='text/javascript' src='/source/jquery.fancybox.js?v=2.0.6'></script><link rel='stylesheet' type='text/css' href='/source/jquery.fancybox.css?v=2.0.6' media='screen' />	<link rel='stylesheet' type='text/css' href='/source/helpers/jquery.fancybox-buttons.css?v=1.0.2' />	<script type='text/javascript' src='/source/helpers/jquery.fancybox-buttons.js?v=1.0.2'></script>	<link rel='stylesheet' type='text/css' href='/source/helpers/jquery.fancybox-thumbs.css?v=1.0.2' />	<script type='text/javascript' src='/source/helpers/jquery.fancybox-thumbs.js?v=1.0.2'></script><script type='text/javascript' src='/source/helpers/jquery.fancybox-media.js?v=1.0.0'></script><script>$(document).ready(function() {$('.fancybox').fancybox({	openEffect	: 'none',	closeEffect	: 'none',nextEffect	: 'none',prevEffect	: 'none'});});</script></head><body>");
            
            ret.append(obj.url);
            
            ret.append("<script>function display(e){var obj=document.getElementById(e);if(obj.style.display=='block'){obj.style.display='none';}else{obj.style.display='block';}}</script>");
            
            abc=new ConsoleFormatter(Q,obj.thumbToUrl);
            
            if(postClusterCoverage>0.0f)abc.setPostClusterCoverage(postClusterCoverage);
            
            abc.setPostProcessingCluster(postProcessingCluster);
            
            abc.setLabelStiching(postProcess);
            
            abc.setLogLookup(logLookup);
            
            abc.setPostProcessingCapCount(postProcessingCapCount);
            
            abc.setReMerge(reMerge);
            
            //setting the galleries flag
            abc.setDispGalleries(galleries);
            
            abc.setGram(grams);
            
            abc.setDebug(debug);
            
            ret.append(abc.displayClusters(clustersByTopic));
            
            ret.append("</body></html>");
            
            return (ret);
            		
		}
		
		public StringBuffer specificQuery(String Q, int L, String on, String source, boolean postProcessingCluster, boolean postProcessingCapCount, boolean intent, int intentLimit,float postClusterCoverage,boolean reMerge,boolean galleries)throws Exception
		{
		
			String query=Q;
            int limit=L;
        
			StringBuffer ret=new StringBuffer();
			
            URLConnectionReader obj = new URLConnectionReader(query,limit,intent);
            
            ArrayList<ArrayList<String>> data1=obj.xmlToDocArray(on, source, intentLimit);
            
            
            
            
            final ArrayList<Document> documents = new ArrayList<Document>();
            
            for (ArrayList<String> row : data1)
            {
                //documents.add(new Document(row[1], row[2], row[0]));
            	documents.add(new Document(row.get(2),row.get(1),row.get(0)));            
            }
            
            hashClustering clustering=new hashClustering(obj.thumbToUrl);
            
            ret.append("<html><head><link rel='stylesheet' type='text/css' href='/lib/style.css'><script type='text/javascript' src='/lib/jquery-1.7.2.min.js'></script><script type='text/javascript' src='/lib/jquery.mousewheel-3.0.6.pack.js'></script><script type='text/javascript' src='/source/jquery.fancybox.js?v=2.0.6'></script><link rel='stylesheet' type='text/css' href='/source/jquery.fancybox.css?v=2.0.6' media='screen' />	<link rel='stylesheet' type='text/css' href='/source/helpers/jquery.fancybox-buttons.css?v=1.0.2' />	<script type='text/javascript' src='/source/helpers/jquery.fancybox-buttons.js?v=1.0.2'></script>	<link rel='stylesheet' type='text/css' href='/source/helpers/jquery.fancybox-thumbs.css?v=1.0.2' />	<script type='text/javascript' src='/source/helpers/jquery.fancybox-thumbs.js?v=1.0.2'></script><script type='text/javascript' src='/source/helpers/jquery.fancybox-media.js?v=1.0.0'></script><script>$(document).ready(function() {$('.fancybox').fancybox({	openEffect	: 'none',	closeEffect	: 'none',nextEffect	: 'none',prevEffect	: 'none'});});</script></head><body>");
            
            ret.append(obj.url);
            
            ret.append("<script>function display(e){var obj=document.getElementById(e);if(obj.style.display=='block'){obj.style.display='none';}else{obj.style.display='block';}}</script>");
            
            ret.append(clustering.cluster(documents));
            
            ret.append("</body></html>");

			return ret;
		}
	
    }
